const helper = new Helper(this, true);
const prefix = helper.getVariable('environmentType');

const { dataSource } = this.widgetObj._option
const env = helper.getVariable('environmentType') || ''
// const api = 'http://192.168.1.100:9098/basics-main/system/equipmentObject/listDetailByEquipmentCode'
const api = `/${env}Basics/basics-main/system/equipmentObject/listDetailByEquipmentCode`
const params = { ...$event }

console.log('dataSource: => ', dataSource)
console.log('params: => ', params)

function isHasData(copyData) {
    while (copyData.length) {
        const item = copyData.pop();
        if (item.catch && item.catch.has(params.id)) {
            return true;
        }
    }
}
function formatData(dataSource, rows) {
    dataSource.forEach(item => {
        if (item.id === params.id) {
            rows.forEach(camera => {
                const { equipmentConfig } = camera
                camera.title = camera.equipmentName
                if (equipmentConfig && equipmentConfig?.gbVideoConfig) {
                    const { sipUser, videoChannelId, sipServerHost, sipServerPort } = equipmentConfig.gbVideoConfig
                    camera.stream_path = `${sipUser}/${videoChannelId}`
                    camera.channelId = videoChannelId
                    camera.deviceId = sipUser
                    camera.host = `${sipServerHost}:${sipServerPort}`
                }
                const index = item.children.findIndex(ev => ev.id === camera.id)
                if(index > -1) {
                    item.children.splice(index, 1, camera)
                } else {
                    item.children.push(camera)
                }
            })
        } else if (item.children?.length) {
            formatData(item.children, rows)
        }
    })
}

const record = RecordServices.getInstance()

if (params.show && !isHasData([...dataSource])) {
    record.getEquipmentListByAreaId(params.id).then(result => {
        if (Array.isArray(result?.rows)) {
            formatData(dataSource, result.rows)
            console.log('formatData', dataSource, result.rows)
            this.widgetObj.setData([{ data: dataSource }])
            this.widgetObj.render()
        }
    })
}
